<template>
	<el-dialog title="发送私信" :visible.sync="visible" width="992px" top="10vh" append-to-body>
		<el-form ref="form" size="medium" :model="letterForm" :rules="letterRules" label-width="80px" class="send_letter_form">
			<el-form-item label="私信标题" prop="title">
				<el-input v-model="letterForm.title"></el-input>
			</el-form-item>
			<el-form-item label="收件人" prop="receivePerson">
				<consignee v-model="letterForm.receivePerson"></consignee>
			</el-form-item>
			<el-form-item label="私信内容" prop="content">
				<u-editor v-model="letterForm.content" :ue-height="400"></u-editor>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSendLetter()">发送</el-button>
			<el-button size="medium" @click="visible = false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import consignee from './consignee';
import { addNotice } from '@/api/front/sysNotice';
export default {
	components: { consignee },
	props: {
		value: { type: Boolean, default: false }
	},
	data() {
		return {
			visible: this.value,
			letterForm: {},
			letterRules: {
				title: [{ required: true, message: '请输入私信标题', trigger: 'blur' }],
				receivePerson: [{ required: true, message: '请选择收件人', trigger: 'change' }],
				content: [{ required: true, message: '请输入私信内容', trigger: 'blur' }]
			},
			btnLoading: false
		};
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		visible(flag) {
			this.$emit('input', flag);
			if (!flag) {
				this.$refs['form'].resetFields();
				this.letterForm = {};
			}
		}
	},
	mounted() {},
	methods: {
		//发送私信
		handleSendLetter() {
			this.$refs['form'].validate((valid) => {
				if (!valid) return false;
				this.btnLoading = true;
				this.sendLetterRequest();
			});
		},

		//私信参数格式化
		formatLetterParams() {
			let params = { ...this.letterForm, type: 2 };
			params['receivePerson'] = params.receivePerson.map((n) => n.relId);
			return params;
		},

		//发送私信请求
		async sendLetterRequest() {
			let params = this.formatLetterParams();
			let result = await addNotice(params).then((res) => res.data);
			this.btnLoading = false;
			if (result.code === '0') {
				this.$message.success('发送成功');
				this.visible = false;
				this.$emit('finish');
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.send_letter_form {
	flex: 1;
	padding: 6px;
	min-height: calc(80vh - 162px);
}
</style>
